//
//  FFObject.m
//  ForceField
//
//  Created by Roxanne Brittain on 11/21/11.
//  Copyright (c) 2011 Digifit. All rights reserved.
//

#import "FFObjectWrapper.h"

@implementation FFObjectWrapper
@synthesize object, view, objectDelegate;

+ (FFObjectWrapper *) objectWithCPPObject:(FFObject *)o
{
	FFObjectWrapper *wrapper = [[FFObjectWrapper alloc] init];
	wrapper.object=o;
    o->observer = wrapper;
	return wrapper;
}

-(void)clear
{
    [self.view removeFromSuperview];
}

-(id)init
{
    self = [super init];
    if (self) {
        view = [[FFCircle alloc] init];
        view.viewDelegate = self;
        object = new FFObject();
    }
    return self;
}

-(void)objectDidUpdate
{
    //set views position and size based on object properties
    if (!object->dragging)
        view.frame = CGRectMake(object->getX(), object->getY(), object->getWidth(), object->getHeight());
    
    FFVector *totalForceVector = new FFVector(0,0);
    for (int i=0; i<object->forceVector.size(); i++) {
        totalForceVector->setX(totalForceVector->getX()+object->forceVector.at(i)->getX());
        totalForceVector->setY(totalForceVector->getY()+object->forceVector.at(i)->getY());
    }
    view.forceVector = totalForceVector;
    
    [view setNeedsDisplay];
}

-(BOOL)viewShouldDrag
{
    return YES;
}
-(void)viewBeganDragging
{
    object->dragging = YES;
}
-(void)viewEndedDragging
{
    object->setPosition(view.frame.origin.x, view.frame.origin.y);
    object->dragging = NO;
}

-(BOOL)viewShouldTap
{
    return [objectDelegate objectShouldTap];
}
-(void)viewDidTap
{
    [objectDelegate objectTapped:self];
}

@end


